import 'package:flutter/material.dart';
import 'package:flutterproject/page/main/main_page.dart';
import 'package:flutterproject/page/mine/mine_page.dart';
import 'package:flutterproject/page/red_package/red_package_page.dart';
import 'package:flutterproject/util/extension/extension_util.dart';
import 'package:flutterproject/util/extension/widget_extension.dart';
import 'package:flutterproject/widget/image_extended.dart';
import 'package:flutterproject/widget/keep_alive_wrapper.dart';
import 'package:flutterproject/widget/text/text_common.dart';
import 'package:get/get.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  int currentSelected = 0;
  PageController controller = PageController();

  @override
  Widget build(BuildContext context) {
    return Container(
      width: Get.width,
      height: Get.height,
      color: Color(0xFFF6F6F6),
      child: Column(
        children: [
          Expanded(
            child: PageView(
              controller: controller,
              physics: const NeverScrollableScrollPhysics(),
              children: [
                KeepAliveWrapper(child: MainPage()),
                RedPackagePage(),
                MinePage(),
              ],
            ),
          ),
          Container(
            height: 120.height,
            color: Colors.white,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildImageText(
                    "icon_home_selected", "icon_home_unselected", "首页", 0),
                ImageCommon(
                  "/image/ic_hb",
                  size: 99.width,
                ).addClickEvent(() {
                  if (currentSelected == 1) return;
                  currentSelected = 1;
                  controller.jumpToPage(1);
                  setState(() {});
                }),
                _buildImageText(
                    "ic_mine_selected", "ic_mine_unselected", "我的", 2),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildImageText(
      String imageSrc, String unselectedImage, String text, int index) {
    return Column(
      children: [
        19.heightBox,
        Image.asset(
          "assets/icon/${index == currentSelected ? imageSrc : unselectedImage}.png",
          width: 48.sp,
          height: 48.sp,
        ),
        10.heightBox,
        TextCommon(
          text,
          color: Color(0xFF414141),
          size: 20.sp,
        )
      ],
    ).addClickEvent(() {
      if (currentSelected == index) return;
      currentSelected = index;
      controller.jumpToPage(index);
      setState(() {});
    });
  }
}
